package cn.tedu.sample.controller;

import cn.tedu.sample.entity.User;
import cn.tedu.sample.mapper.UserMapper;
import cn.tedu.sample.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("handle_reg")
    public JsonResult handleReg(User user) {
        try {
            User u = userMapper.findUserByUsername(user.getUsername());
            if (u != null) {
                return new JsonResult(-1, "用户名已经注册!");
            }
            int n = userMapper.addnew(user);
            if (n == 1) {
                return new JsonResult(1, "注册成功!");
            } else {
                return new JsonResult(0, "注册失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(0, "注册失败!");
        }
    }
}
